﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.Collections;
using System.Xml;
using System.Linq;
//using System.Xml.Linq;

using CommonUtils;
using TDAL = EzDAL.Auto2;
using TDALUtils = EzDAL.Utils;
using TConst = TERP.Constants;
using TUtils = TERP.Utils;
using TError = TERP.Errors;


namespace TERP.BizITS
{
    public partial class BizWater
	{
		#region // Private Utils:
		#endregion

        #region //Validate

        #region //Price
		#region //CheckExistUpdatePriceUnitCode
		private void CheckExistUpdatePriceUnitCode(
           ref ArrayList alParamsCoupleError
           , string strUnitCode
           , out DataTable dtCus
        )
        {
			dtCus = TDALUtils.DBUtils.GetTableContents(
                _dbCarSv // db
                , "Water_Price" // strTableName
                , "top 1 *" // strColumnList
                , "" // strClauseOrderBy
				, "UnitCode", "=", strUnitCode // arrobjParamsTriple item
             );

			if (dtCus == null || dtCus.Rows.Count == 0)
            {

                throw CMyException.Raise(
					TError.ErrITS.Its_Price_ID_NotFound
                    , null
                    , alParamsCoupleError.ToArray()
                    );
            }
        }
        #endregion
        #endregion


        #endregion

        #region //Water Price

		#region Its_Price_Get
		public DataSet Its_Price_Get(
		string strGWUserCode
		, string strGWPassword
		, string strTid
		, string strPartnerCode
		, string strPartnerUserCode
		, string strLanguageCode
		, string strUnitCodeConditionList
		)
		{

			#region // Temp:

			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);

			int nTidSeq = 0;
			bool bNeedTransaction = false;
			string strFunctionName = "Its_Price_Get";
			string strErrorCodeDefault = TError.ErrITS.Its_Price_Get;
			ArrayList alParamsCoupleError = new ArrayList(new object[]{

                    "strFunctionName", strFunctionName
                    , "strUnitCodeConditionList", strUnitCodeConditionList
                    });

			#endregion

			try
			{

				#region // Init:

				_dbCarSv.AppUserID = strPartnerUserCode;

				if (bNeedTransaction) _dbCarSv.BeginTransaction();

				// Write request log:

				_log.WriteLogAsync(

				_strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strTid // strAppTid

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUPartCode

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, TError.Error.NoError // strAppErrorCode

				, strFunctionName // strAppLogType1

				, "REQUEST" // strAppLogType2

				, "" // strAppLogType3

				, strLanguageCode // strAppLanguageCode

				, "" // strAppRemark

				, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple

				);

				// Check Valid Tid:

				myUtils_ValidateId(

				ref alParamsCoupleError // alParamsCoupleError

				, strTid // strTid

				, TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid

				);

				#endregion

				#region // Build Sql:

				ArrayList alParamsCoupleSql = new ArrayList();

				string strSqlGetData = @"
						SELECT
                               t.*
                        FROM Water_Price t with (nolock)
						WHERE (1=1)
                                    and UnitCode='@UnitCode'
                                   
									;
				";

				strSqlGetData = StringUtils.Replace(
				strSqlGetData
				, "@UnitCode", strUnitCodeConditionList
				
				);

				#endregion

				#region // Get Data:
				myDebug_SaveSql(
					_bDebugMode // bDebugMode
					, ref alParamsCoupleError // alParamsCoupleError
					, string.Format("strSql_{0}", strFunctionName) // strParamName
					, strSqlGetData // strSql
					);


				DataSet dsGetData = _dbCarSv.ExecQuery(

				strSqlGetData

				, alParamsCoupleSql.ToArray()

				);

				dsGetData.Tables[0].TableName = "Water_Price";

				DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

				#endregion

				// Return Good:

				TDALUtils.DBUtils.CommitSafety(_dbCarSv);

				mdsFinal.AcceptChanges();

				return mdsFinal;

			}

			catch (Exception exc)
			{

				#region // Catch of try:

				// Rollback:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				// Return Bad:

				return TUtils.CProcessException.Process(

				ref mdsFinal

				, exc

				, strErrorCodeDefault
				, alParamsCoupleError.ToArray()

				);

				#endregion

			}

			finally
			{

				#region // Finally of try:

				// Rollback and Release resources:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

				TUtils.CProcessException.Process(

				ref mdsFinal // mdsFinal

				, _log // log

				, _strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strFunctionName // strFunctionName

				, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUserCode

				, strLanguageCode // strLanguageCode

				, "" // strRemark"

				);

				#endregion

			}

		}

		#endregion


		#region //Its_Price_Update
		public DataSet Its_Price_Update(
			 string strGWUserCode
			 , string strGWPassword
			 , string strTid
			 , string strPartnerCode
			 , string strPartnerUserCode
			 , string strLanguageCode
			 , string strUnitCode
			 , string strDVTinh
			 , string strPrice
			 , string strDateUpdate
     )
        {
            #region // Temp:
            DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
            int nTidSeq = 0;
            bool bNeedTransaction = true;
			string strFunctionName = "Its_Price_Update";
			string strErrorCodeDefault = TError.ErrITS.Its_Price_Update;
            ArrayList alParamsCoupleError = new ArrayList(new object[]{
			"strFunctionName", strFunctionName
			,"strUnitCode",strUnitCode
			, "strDVTinh", strDVTinh
			, "strPrice", strPrice
			, "strDateUpdate", strDateUpdate
			});
            #endregion
            try
            {
                #region // Init:
                _dbCarSv.AppUserID = strPartnerUserCode;
                if (bNeedTransaction) _dbCarSv.BeginTransaction();
                // Write request log:
                _log.WriteLogAsync(
                _strConfig_GwUserCode // strGwUserCodeForLog
                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                , strTid // strAppTid
                , strPartnerCode // strAppServiceCode
                , strPartnerUserCode // strAppUPartCode
                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                , TError.Error.NoError // strAppErrorCode
                , strFunctionName // strAppLogType1
                , "REQUEST" // strAppLogType2
                , "" // strAppLogType3
                , strLanguageCode // strAppLanguageCode
                , "" // strAppRemark
                , TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
                );
                // Check Valid Tid:
                myUtils_ValidateId(
                ref alParamsCoupleError // alParamsCoupleError
                , strTid // strTid
                , TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid
                );
                #endregion

                #region // Check:
                DataTable dt_Book = null;
				this.CheckExistUpdatePriceUnitCode(
                    ref alParamsCoupleError
                    , strUnitCode
                    , out dt_Book
                    );
                #endregion

                #region // dt_Book:
                ArrayList alColumnEffective = new ArrayList();
                alColumnEffective.Clear();

				//dt_Book.Rows[0]["Unit"] = strCusCode; alColumnEffective.Add("CusCode");
				if (!StringUtils.IsEmpty(strDVTinh))
				{
					dt_Book.Rows[0]["DVTinh"] = strDVTinh; alColumnEffective.Add("DVTinh");
				}

				if (!StringUtils.IsEmpty(strPrice))
				{
					dt_Book.Rows[0]["Price"] = strPrice; alColumnEffective.Add("Price");
				}

				if (!StringUtils.IsEmpty(strDateUpdate))
				{
					dt_Book.Rows[0]["DateUpdate"] = strDateUpdate; alColumnEffective.Add("DateUpdate");
				}

                //Save
                _dbCarSv.SaveData("Water_Price", dt_Book, alColumnEffective.ToArray());

                //Get ID
                
                #endregion

                #region // Return data
          
                //Return data
				DataSet dsGetData = new DataSet();
                dsGetData.Tables.Add(dt_Book.Copy());
				dsGetData.Tables[0].TableName = "Water_Price";

                DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

                #endregion

                // Return Good:
                TDALUtils.DBUtils.CommitSafety(_dbCarSv);
                mdsFinal.AcceptChanges();
                return mdsFinal;
            }
            catch (Exception exc)
            {
                #region // Catch of try:
                // Rollback:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                // Return Bad:
                return TUtils.CProcessException.Process(
                ref mdsFinal
                , exc
                , strErrorCodeDefault
                , alParamsCoupleError.ToArray()
                );
                #endregion
            }
            finally
            {
                #region // Finally of try:
                // Rollback and Release resources:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);
                TUtils.CProcessException.Process(
                ref mdsFinal // mdsFinal
                , _log // log
                , _strConfig_GwUserCode // strGwUserCodeForLog
                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                , strFunctionName // strFunctionName
                , TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                , strPartnerCode // strAppServiceCode
                , strPartnerUserCode // strAppUserCode
                , strLanguageCode // strLanguageCode
                , " // strRemark"
                );
                #endregion
            }
        }

        #endregion

		

        #endregion
        
    }
}
